<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 消息控制器-B端
 *
 * @desc    消息控制器
 * @version 1.0.0 直接完善文档注释
 * @author  Jason
 * @date	2017年7月21日 下午3:42:31
 * 
 * @title 消息管理
 * @entry http://discovery.segway.com
 */
class Message extends MY_Business_Controller
{
	/** @var Lib_message */
	public $lib_message = null;
	
	public function __construct()
	{
		parent::__construct();
		
		$this->load->library('lib_message');
		
		$this->set_mutable_view_var('head.title', 'Message - Business Management');
		$this->set_mutable_view_var('head.keywords', '');
		$this->set_mutable_view_var('head.description', '');
		
		$this->lang->load('message');
	}
	
	
	/**
	 * 我发出的消息列表页
	 *
	 * @author  Jason
	 * @date	2017年7月24日 下午3:03:08
	 * @link    /business/message/listing_mine
	 */
	public function listing_mine($page = 1, $number = 20)
	{
		$member_id = $this->get_logined_member()['id'];
		$conditions = $this->lib_message->get_conditions();
		$conditions[] = ['where', "target_id=$member_id"];
		$list = $this->lib_message->get_messages_for_list($conditions, $page, $number);
		$data['list'] = $list;
		
		$this->load->view('manager/message/list_mine', $data);
	}
	
	/**
	 * 我收到的消息列表页
	 *
	 * @author  Jason
	 * @date	2017年7月24日 下午3:03:08
	 * @link    /business/message/listing_container_mine
	 */
	public function listing_container_mine($page = 1, $number = 20)
	{
		$member_id = $this->get_logined_member()['id'];
		$this->lib_message->receive_new_messages($member_id);
		$this->lib_message->receive_new_messages($member_id, 'APP');
		$conditions = $this->lib_message->get_conditions();
		$conditions[] = ['where', "target_id=$member_id"];
		$conditions[] = ['order_by', "message_id DESC"];
		$list = $this->lib_message->get_containers_for_list($conditions, $page, $number);
		$data['list'] = $list;
		
		$this->load->view('manager/message/list_mine', $data);
	}
	
	
	/**
	 * 消息状态切换 - 已读/未读（read），保留/删除（retained）
	 *
	 * @desc	消息状态切换，可以切换消息的各种状态！
	 * @method	POST
	 * @author	Jason
	 * @date	2017年7月24日 下午3:39:59
	 *
	 * @param	number $id	消息ID
	 * @param	string $tag	消息正向标签，已读(read)、未删/保留(retained)
	 * 
	 * @param	string $mapping	超级管理员专用参数！
	 *
	 * @return	JSON	标准JSON格式响应
	 *
	 * @link	/manager/message/ajax_toggle_status
	 *
	 * @command noneurl
	 */
	public function ajax_toggle_status()
	{
		$mapping = 'container';
		list($status, $content) = $this->lib_message->toggle_status_positive_tag($mapping);
		
		if(!$status) {
			Lib_json::renderError(Lib_json::ERROR_BUSINESS, current($content));
		} else {
			Lib_json::renderSuccess(['data' => $content]);
		}
	}
	
	
}



